<?php
class ProductController extends AdminAppController{
    
    
    
    function index($parent_id = null)
    {
        $this->loadModel('Admin.Product');
        $product = $this->Product->get_product('all', $parent_id);

        $list_product = array();
        foreach ($product as $pro) {
            $temp = array();
            $temp['product_id'] = $pro['product']['product_id'];
            $temp['title'] = $pro['product_desc']['title'];
            $temp['sort_order'] = $pro['product']['sort_order'];
            $temp['status'] = $pro['product']['status'];
            $temp['image'] = $pro['image']['name'];
            $temp['category_name'] = $pro['Custom']['category_name'];
            $temp['favorite'] = $pro['product']['favorite'];
            $temp['in_stock'] = $pro['product']['in_stock'];
            $temp['type'] = $pro['product']['type'];
            $list_product[] = $temp;
        }
        $this->set('list_product', $list_product);
        
        $parent_category = $this->Product->get_parent_category();
        $this->set('parent_category', $parent_category);
        $this->set('parent_id', $parent_id);
    }
    
    function add($parent_id = null)
    {
        $this->loadModel('Admin.Language');
        $this->loadModel('Admin.Category');
        $this->loadModel('Admin.Brand');
        
        $list_language = $this->Language->get_language();
        $this->set('list_language', $list_language);
        
        
        $category = $this->Category->get_all_category();
        $filter_category = $this->Category->filter_category($category);
        $this->set('filter_category', $filter_category);

        $this->set('parent_id', $parent_id);
        
        $brands = $this->Brand->get_brand();
        $list_brands = array();
        foreach($brands as $brand)
        {
            $list_brands[$brand['Brand']['brand_id']] = $brand['Brand']['title'];
        }
        $this->set('list_brands', $list_brands);
        
        if($this->request->isPost())
        {
            $this->loadModel('Admin.Product');
            $this->loadModel('Admin.ProductDesc');
            $this->loadModel('Admin.ProductCategory');
            $this->loadModel('Admin.Image');
            
            $data = array(
                        'brand_id' => $this->request->data['Product']['brand_id'],
                        'date_added' => date("Y-m-d H:i:s"),
                        'date_modified' => date("Y-m-d H:i:s"),
                        'status' => $_POST['status'],
                        'price' => $this->request->data['Product']['price'],
                        'discount' => $this->request->data['Product']['discount'],
                        'percent_discount' => $this->request->data['Product']['percent_discount'],
                        'type' => $this->request->data['Product']['type'],
                        'in_stock' => 1,
                        'sort_order' => $this->request->data['Product']['sort_order'],
                        );
            $product_id = $this->Product->add($data);

            foreach($list_language as $language)
            {
                $language = $language['language'];
                $lang_code = $language['lang_code'];
                $data = array(
                        'product_id' => $product_id,
                        'title' => $this->request->data['Product']['title_'.$lang_code],
                        'description' => $this->request->data['Product']['description_'.$lang_code],
                        'long_description' => $this->request->data['Product']['long_description_'.$lang_code],
                        'meta_title' => $this->request->data['Product']['meta_title_'.$lang_code],
                        'meta_description' => $this->request->data['Product']['meta_description_'.$lang_code],
                        'meta_keywords' => $this->request->data['Product']['meta_keywords_'.$lang_code],
                        'lang_code' => $lang_code,
                        );
                $this->ProductDesc->add($data);
            }
            
            $main_image = $_POST['main_image'];
            $other_image = isset($_POST['other_image']) ? $_POST['other_image'] : array();
            $order_image = isset($_POST['order_image']) ? $_POST['order_image'] : array();
            $data['main_image'] = $main_image;
            $data['other_image'] = $other_image;
            $data['order_image'] = $order_image;
            $this->Image->add($product_id, 'product', $data);
            
            $main_category = $this->request->data['main_category'];
            $product_category = $this->request->data['product_category'];
            foreach($product_category as $value)
            {
                $data = array();
                $data['product_id'] = $product_id;
                $data['category_id'] = $value;
                if($value == $main_category)
                    $data['main_category'] = '1';
                $this->ProductCategory->add($data);
            }
            
            $this->loadModel('Admin.Link');
            foreach($list_language as $language)
            {
                $language = $language['language'];
                $lang_code = $language['lang_code'];
                
                $data = array(
                        'link' => $this->request->data["Product"]["link_$lang_code"],
                        'controller' => 'product',
                        'method' => 'index',
                        'post_id' => $product_id,
                        'type' => 'product',
                        'lang_code' => $lang_code
                );
                $this->Link->add($data);
            }
            
            $this->Session->setFlash(
                                'Thêm mới sản phẩm thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
            
            $this->redirect(array("controller" => "product", 
                                  "action" => "index", $parent_id));
        }
    }
    
    function edit($product_id = "", $parent_id = null)
    {
        if($product_id == ""){
            $this->redirect(array("controller" => "product", 
                                  "action" => "index")
                            );
            exit;
        }
        $this->set('product_id', $product_id);
        $this->loadModel('Admin.Language');
        $this->loadModel('Admin.Category');
        $this->loadModel('Admin.Image');
        $this->loadModel('Admin.Product');
        $this->loadModel('Admin.ProductDesc');
        $this->loadModel('Admin.ProductCategory');
        $this->loadModel('Admin.Brand');
        
        $product_info = $this->Product->get_product_by_id($product_id, 'all');

        $this->set('product_info', $product_info);
        
        $list_language = $this->Language->get_language();
        $this->set('list_language', $list_language);

        $category = $this->Category->get_category();
        $list_category = array();
        foreach ($category as $cat) {
            $list_category[$cat['category']['category_id']] = $cat['category_desc']['title'];
        }
        $this->set('list_category', $list_category);
        
        $list_image = $this->Image->get($product_id, 'product');
        $this->set('list_image', $list_image);
        
        $category = $this->Category->get_all_category();
        $filter_category = $this->Category->filter_category($category);
        $this->set('filter_category', $filter_category);
        
        $product_category = $this->ProductCategory->get_by_product_id($product_id);
        $this->set('product_category', $product_category);
        
        $this->set('parent_id', $parent_id);
        
        $brands = $this->Brand->get_brand();
        $list_brands = array();
        foreach($brands as $brand)
        {
            $list_brands[$brand['Brand']['brand_id']] = $brand['Brand']['title'];
        }
        $this->set('list_brands', $list_brands);
        
        if($this->request->isPost())
        {
            $data = array(
                        'brand_id' => "'".$this->request->data['Product']['brand_id']."'",
                        'date_modified' => "'".date("Y-m-d H:i:s")."'",
                        'status' => "'".$_POST['status']."'",
                        'price' => "'".$this->request->data['Product']['price']."'",
                        'discount' => "'".$this->request->data['Product']['discount']."'",
                        'percent_discount' => "'".$this->request->data['Product']['percent_discount']."'",
                        'type' => "'".$this->request->data['Product']['type']."'",
                        'in_stock' => 1,
                        'sort_order' => "'".$this->request->data['Product']['sort_order']."'",
                        );
            $this->Product->edit($product_id, $data);

            foreach($list_language as $language)
            {
                $language = $language['language'];
                $lang_code = $language['lang_code'];
                $data = array(
                        'title' => "'".$this->request->data['Product']['title_'.$lang_code]."'",
                        'description' => "'".$this->request->data['Product']['description_'.$lang_code]."'",
                        'long_description' => "'".$this->request->data['Product']['long_description_'.$lang_code]."'",
                        'meta_title' => "'".$this->request->data['Product']['meta_title_'.$lang_code]."'",
                        'meta_description' => "'".$this->request->data['Product']['meta_description_'.$lang_code]."'",
                        'meta_keywords' => "'".$this->request->data['Product']['meta_keywords_'.$lang_code]."'",
                        );
                $this->ProductDesc->edit($product_id, $lang_code, $data);
            }
            
            $main_image = $_POST['main_image'];
            $other_image = isset($_POST['other_image']) ? $_POST['other_image'] : array();
            $order_image = isset($_POST['order_image']) ? $_POST['order_image'] : array();
            $data['main_image'] = $main_image;
            $data['other_image'] = $other_image;
            $data['order_image'] = $order_image;
            $this->Image->edit($product_id, 'product', $data);
            
            $main_category = $this->request->data['main_category'];
            $product_category = $this->request->data['product_category'];
            $this->ProductCategory->del($product_id);
            foreach($product_category as $value)
            {
                $data = array();
                $data['product_id'] = $product_id;
                $data['category_id'] = $value;
                if($value == $main_category)
                    $data['main_category'] = '1';
                $this->ProductCategory->add($data);
            }
            
            $this->loadModel('Admin.Link');
            $this->Link->deleteAll(array('post_id' => $product_id, 'type' => 'product'));
            foreach($list_language as $language)
            {
                $language = $language['language'];
                $lang_code = $language['lang_code'];
                
                $data = array(
                        'link' => $this->request->data["Product"]["link_$lang_code"],
                        'controller' => 'product',
                        'method' => 'index',
                        'post_id' => $product_id,
                        'type' => 'product',
                        'lang_code' => $lang_code
                );
                $this->Link->add($data);
            }
            
            $this->Session->setFlash(
                                'Sửa sản phẩm thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
            
            $this->redirect(array("controller" => "product", 
                                  "action" => "index", $parent_id));
        }
    }
    
    function compo($product_id = null, $type = null, $parent_id = null)
    {
        if($product_id == ""){
            $this->redirect(array("controller" => "product", 
                                  "action" => "index")
                            );
            exit;
        }
        $this->set('product_id', $product_id);
        $this->set('parent_id', $parent_id);
        
        $this->loadModel('Admin.Composition');
        $this->loadModel('Admin.Language');
        $this->loadModel('Admin.ProductCompo');
        $list_language = $this->Language->get_language();
        $this->set('list_language', $list_language);
        
        $list_compo = $this->Composition->get_compo_by_group($type);
        $this->set('list_compo', $list_compo);
        
        $product_compo = $this->ProductCompo->get_compo_by_product_id($product_id);
        $this->set('product_compo', $product_compo);
        if($this->request->isPost())
        {
            $compo= $_POST['compo'];
            $this->ProductCompo->del($product_id);
            foreach($compo as $lang_code => $composition)
            {
                foreach($composition as $compo_id =>$content)
                {
                    $data['product_id'] = $product_id;
                    $data['compo_id'] = $compo_id;
                    $data['content'] = $content;
                    $data['lang_code'] = $lang_code;
                    $this->ProductCompo->add($data);
                }
            }
            
            $this->redirect(array("controller" => "product", 
                                  "action" => "index", $parent_id)
                            );
        }
    }
    
    function change_favorite()
    {
        $this->autoRender = false;
        $product_id = $_POST['product_id'];
        $change_favorite = $this->Product->change_favorite($product_id);
        echo $change_favorite;
    }
    
    function change_in_stock()
    {
        $this->autoRender = false;
        $product_id = $_POST['product_id'];
        $change_in_stock = $this->Product->change_in_stock($product_id);
        echo $change_in_stock;
    }
    
}
